<?php
namespace app\api\model;
use think\Db;
use traits\model\SoftDelete;

class UserMoneyOrder extends Base
{
    use SoftDelete;
    // 设置当前模型的数据库连接
    public function OrderNotify($post,$pay_style,$source){
        $order = (new UserMoneyOrder())->findOne([['order_number','eq',$post['out_trade_no']]]);
        if($order['is_pay']==1){
            $new =[
                'id'=>$order['id'],
                'is_pay'=>2,
                'pay_style'=>$pay_style,
                'source' =>$source,
                'pay_time'=>time(),
            ];
            Db::startTrans();
            $res =(new UserMoneyOrder())->dataUpdate($new);
            if($res){//给用户加VIP时间

                $user = (new User())->findOne([['id','eq',$order['user_id']]]);
                $new =[
                  'id'=>$user['id']
                ];
                if($user['user_type']==2||$user['user_type']==4){
                    $new['vip_time'] = $user['vip_time']+$order['give_day']*86400;
                }else{
                    $new['vip_time'] = time()+$order['give_day']*86400;
                    if($user['user_type']==1){
                        $new['user_type']=2;
                    }else{
                        $new['user_type']=4;
                    }
                }
                $res = (new User())->dataUpdate($new);
                if(!$res){
                    Db::rollback();
                }
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }
}